package com.cherish.meeting.api.service.impl;

import com.cherish.meeting.api.service.IMeetingRecordService;
import com.cherish.meeting.common.constant.MeetingStatus;
import com.cherish.meeting.common.exception.BussException;
import com.cherish.meeting.common.util.CalendarUtil;
import com.cherish.meeting.common.util.CollectionUtil;
import com.cherish.meeting.common.vo.MeetingRecordVo;
import com.cherish.meeting.db.entity.MeetingRecord;
import com.cherish.meeting.db.mapper.MeetingRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-03-02
 */
@Service
public class MeetingRecordServiceImpl implements IMeetingRecordService {

    @Autowired
    private MeetingRecordMapper meetingRecordMapper;

    @Override
    public List<MeetingRecord> getMyMeetings(String openid) {
        MeetingRecord record = new MeetingRecord();
        record.setOpenid(openid)
                .setMeetingDate(new java.sql.Date(System.currentTimeMillis()))
                .setEndTime(CalendarUtil.getNowSqlTime());
        return meetingRecordMapper.getMyMeeting(record);
    }

    @Override
    public List<MeetingRecord> getMeetingByRoom(MeetingRecord record) {
        record.setMeetingDate(new java.sql.Date(System.currentTimeMillis()))
                .setEndTime(CalendarUtil.getNowSqlTime());
        return meetingRecordMapper.getMeetingByRoom(record);
    }

    @Override
    public String bookMeeting(MeetingRecord record) {
        List<MeetingRecord> meetings = meetingRecordMapper.bookRoomCheck(record);
        if (!CollectionUtil.isEmpty(meetings)) {
            return "会议时间冲突，请重新选择！";
        }
        int result = meetingRecordMapper.insert(record);
        return result == 1 ? MeetingStatus.BOOK_SUCCESS : MeetingStatus.BOOK_ERROR;
    }
}
